﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace Skynet.WPF4.Views.MultiTouch
{
    public class ImageTouch : Image
    {
        private MatrixTransform _MatrixTransform;
        public ImageTouch()
        {
            _MatrixTransform = new MatrixTransform();            
            this.RenderTransform = _MatrixTransform; 
        }
        protected override void OnManipulationDelta(System.Windows.Input.ManipulationDeltaEventArgs e)
        {            
            Matrix matrix = this._MatrixTransform.Matrix;
            var originalCentre = new Point(ActualWidth / 2, ActualHeight / 2);

            matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
            matrix.Transform(originalCentre);

            var centre = matrix.Transform(originalCentre);
            matrix.RotateAt(e.DeltaManipulation.Rotation, centre.X, centre.Y);

            centre = matrix.Transform(originalCentre);
            matrix.ScaleAt(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.Y, centre.X, centre.Y);

            this._MatrixTransform.Matrix = matrix;
            e.Handled = true;
            base.OnManipulationDelta(e);

        }
    }
}
